import datetime
import logging
import signal
from absl import app, flags

from coin.strategy.hk_hard_arb.util import load_clean_json
from coin.strategy.hk_lm.model_fast_profile.get_profile import get_fs_config
from coin.strategy.hk_lm.lm_pass_strategy import (LmPassStrategyConfig, LmPassStrategy)

FLAGS = flags.FLAGS


def main(argv):
  fs_config = get_fs_config()
  init_og_after_min = 0.1
  exit_after_min = init_og_after_min + FLAGS.exit_after_min

  strat_config = LmPassStrategyConfig(load_clean_json(FLAGS.config_filename))
  strategy = LmPassStrategy(strat_config, dry_run=False)

  signal.signal(signal.SIGTERM, strategy.signal_handler)
  signal.signal(signal.SIGINT, strategy.signal_handler)

  return strategy.run_live(strategy=strategy,
                           on_order_gateway_reset_callback=strategy.on_og_reset,
                           og_config_name=FLAGS.og_config_name,
                           og_params=strategy.config.og_params,
                           init_order_gateway_after=datetime.timedelta(minutes=init_og_after_min),
                           exit_after=datetime.timedelta(minutes=exit_after_min),
                           fs_config=fs_config,
                           og_log_callback=strategy.strategy_logger.write_og_log)


if __name__ == '__main__':
  flags.DEFINE_string('og_config_name', None, 'og_config_name')

  flags.DEFINE_string('config_filename', '', '')

  flags.DEFINE_integer('exit_after_min', 30, '')

  logging.basicConfig(level='INFO', format='%(levelname)s %(asctime)s %(name)s] %(message)s')
  app.run(main)
